package com.bg.print.controller;

import com.bg.print.entity.FileEntity;
import com.bg.print.service.FileService;
import com.bg.print.utils.PageUtils;
import com.bg.print.utils.R;
import com.bg.print.variable.Enabled;
import io.minio.errors.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Map;



/**
 * 
 *
 * @author liuxvru
 * @email liuxvru@163.com
 * @date 2024-03-25 17:50:46
 */

@Tag(name = "文件")
@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    private FileService fileService;

    /**
     * 列表
     */
    @Operation(summary = "列表")
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('ADMIN')")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = fileService.queryPage(params);
        return R.ok().put("data", page);
    }


    /**
     * 信息
     */
    @Operation(summary = "信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAuthority('ADMIN')")
    public R info(@PathVariable("id") Long id){
		FileEntity file = fileService.getById(id);
        return R.ok().put("data", file);
    }

//    /**
//     * 保存
//     */
//    @Operation(summary = "保存")
//    @PostMapping("/save")
//    //@RequiresPermissions(":file:save")
//    public R save(@RequestBody FileEntity file){
//        file.setEnabled(Enabled.USING.getCode());
//		fileService.save(file);
//        return R.ok();
//    }

//    /**
//     * 修改
//     */
//    @Operation(summary = "修改")
//    @PutMapping("/update")
//    //@RequiresPermissions(":file:update")
//    public R update(@RequestBody FileEntity file){
//		fileService.updateById(file);
//        return R.ok();
//    }

//    /**
//     * 删除
//     */
//    @Operation(summary = "删除")
//    @DeleteMapping("/delete")
//    //@RequiresPermissions(":file:delete")
//    public R delete(@RequestBody Long[] ids){
//		fileService.removeByIds(Arrays.asList(ids));
//        return R.ok();
//    }

    /**
     * Get
     */
    @Operation(summary = "文件名获取签名地址直传minio文件服务器")
    @GetMapping("/token")
    public R minioPut(@RequestParam String fileName) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        String url = fileService.getToken(fileName);
        return R.ok().put("data",url);
    }


}
